package com.sskj.qiangjin.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.stetho.inspector.elements.Document;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.BillRecordBean;
import com.sskj.qiangjin.bean.RechargeListBean;
import com.sskj.qiangjin.bean.WalletBean;
import com.sskj.qiangjin.bean.rxbus.AddressBean;
import com.sskj.qiangjin.presenter.WalletAddressManagerPresenter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * Created by QinKang on 2019/1/1.
 */

@Route(path = RConfig.APP_WALLET_ADDRESS_MANAGER)//钱包地址管理
public class WalletAddressManagerActivity extends BaseActivity<WalletAddressManagerPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_add_wallet_address)
    Button btnAddWalletAddress;
    private SlimAdapter slimAdapter;
    @Autowired
    boolean isWithDraw;
    private List<WalletBean> walletBeans = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_wallet_address_manager;
    }

    @Override
    public WalletAddressManagerPresenter getPresenter() {
        return new WalletAddressManagerPresenter();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mPresenter.getWalletList();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.app_activity_wallet_address_manager));
        ARouter.getInstance().inject(this);
        mPresenter.getWalletList();
        ClickUtil.click(btnAddWalletAddress, () -> {
            ARouter.getInstance().build(RConfig.APP_WALLET_ADDRESS_ADD).navigation();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_wallet_address_manager, new SlimInjector<WalletBean>() {
            @Override
            public void onInject(WalletBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_wallet_remark, data.getRemark())
                        .text(R.id.tv_wallet_address, data.getAddress())
                        .clicked(R.id.tv_wallet_address, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (isWithDraw) {
                                    RxBus.getDefault().post(new AddressBean(data.getAddress()));
                                    finish();
                                }
                            }
                        })
                        .clicked(R.id.tv_delete_wallet, view -> mPresenter.deleteWallet(data.getId()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

//        ItemClickSupport.addTo(recyclerView)
//                .setOnItemClickListener((recyclerView, position, v) -> {
//                    if (isWithDraw) {
//                        RxBus.getDefault().post(new AddressBean(slimAdapter.getDataItem(position).toString()));
//                        finish();
//                    }
//                });

//        Flowable.rangeLong(0, 8)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> slimAdapter.updateData(strings));

    }

    public void delSuccess() {
        mPresenter.getWalletList();
    }

    public void updateUI(List<WalletBean> datas) {
        walletBeans.clear();
        walletBeans.addAll(datas);
        slimAdapter.updateData(walletBeans);
    }
}
